//***************************************************************************
//*
//*	File:			DialogConfigure.cpp
//*	Description:	Dialog for managing configurations
//*
//***************************************************************************

//
//--------------------------------------------------------------- PRECOMPILED
//

#include "stdafx.h"

//
//--------------------------------------------------- DECLARATION DEPENDENCIES
//

#include "BuildMgr2.h"

//
//--------------------------------------------------------------- DECLARATION
//

#include "DialogConfigure.h"

//
//--------------------------------------------------- DEFINITION DEPENDENCIES
//

#include "DialogProjects.h"

//
//-------------------------------------------------------------- PREPROCESSOR
//

#ifdef	_DEBUG
#define new DEBUG_NEW
#undef	THIS_FILE
static	char THIS_FILE[] = __FILE__;
#endif

//
//---------------------------------------------------------------- DEFINITION
//

//
//---------------------------------------------------------------------------------------------------
//*************************************     CON/DESTRUCTION     *************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																					  Constructor
//***************************************************************************************************
//**	@DOC		CONSTRUCTION
//**	@MFUNC		Default constructor
//**	@PARM		[in|opt] Pointer to the parent window
//**	@END
//***************************************************************************************************
//inline
CDialogConfigure::CDialogConfigure(CWnd* pParent /*=NULL*/)
: CDialog(CDialogConfigure::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialogConfigure)
	m_iFinishedAction		= 0		;
	//}}AFX_DATA_INIT

	this->m_pIApplication	= NULL	;
}

//
//---------------------------------------------------------------------------------------------------
//*****************************************     GET/SET     *****************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																				   SetApplication
//***************************************************************************************************
//**	@DOC		SET
//**	@MFUNC		Sets the pointer to the application's interface
//**	@PARM		[in] Pointer to application's interface
//**	@END
//***************************************************************************************************
//inline
void CDialogConfigure::SetApplication( IApplication *a_pIApplication )
{
	this->m_pIApplication = a_pIApplication;
}

//
//---------------------------------------------------------------------------------------------------
//***************************************     PERSISTENCE     ***************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																					  LoadProfile 
//***************************************************************************************************
//**	@DOC		PERSISTENCE
//**	@MFUNC		Loads the dialog's settings from the registry
//**	@END
//***************************************************************************************************
//inline
void CDialogConfigure::LoadProfile( void )
{
	//
	//	GET MEMBERS
	//
	this->m_bSendMail			= (BOOL)(	::AfxGetApp()->GetProfileInt	(_T("Configure"), _T("SendMail"				), 0		) )	;
	this->m_strSendTo			=			::AfxGetApp()->GetProfileString	(_T("Configure"), _T("SendTo"				), _T("")	)	;
	this->m_strStatusLine		=			::AfxGetApp()->GetProfileString	(_T("Configure"), _T("StatusText"			), _T("")	)	;
	this->m_bMailOnEach			= (BOOL)(	::AfxGetApp()->GetProfileInt	(_T("Configure"), _T("MailOnEach"			), 1		) )	;
	this->m_bIncBuildResults	= (BOOL)(	::AfxGetApp()->GetProfileInt	(_T("Configure"), _T("IncludeBuildResults"	), 1		) )	;
	this->m_bSendFile			= (BOOL)(	::AfxGetApp()->GetProfileInt	(_T("Configure"), _T("SendFile"				), 0		) )	;
	this->m_strFileToSend		=			::AfxGetApp()->GetProfileString	(_T("Configure"), _T("FileToSend"			)			)	;
	this->m_iFinishedAction		=			::AfxGetApp()->GetProfileInt	(_T("Configure"), _T("FinishedAction"		), 0		) 	;

	//
	//	UPDATE CONTROLS
	//
	this->UpdateData( FALSE );
}

//***************************************************************************************************
//**																					 WriteProfile 
//***************************************************************************************************
//**	@DOC		PERSISTENCE
//**	@MFUNC		Saves the dialog's settings to the registry
//**	@RDESC		<t TRUE> if successful, <t FALSE> otherwise.
//**	@END
//***************************************************************************************************
//inline
BOOL CDialogConfigure::WriteProfile( void ) const
{
	//
	//	GET NON-CONST POINTER TO THIS 
	//
	CDialogConfigure* pThis = const_cast< CDialogConfigure* > (this);

	//
	//	GET VALUES FROM CONTROLS
	//
	pThis->UpdateData( TRUE );

	//
	//	UPDATE MEMBERS
	//
	AfxGetApp()->WriteProfileInt(_T("Configure"), _T("SendMail"), m_bSendMail);

	pThis->m_SendTo.GetWindowText( pThis->m_strSendTo );

	if (m_SendMail.GetCheck() == 1)
	{
		if (m_strSendTo == _T(""))
		{
			AfxMessageBox( IDS_NEEDMAILADDR );

			return FALSE;
		}
	}

	pThis->m_StatusLine.GetWindowText( pThis->m_strStatusLine );

	pThis->m_bIncBuildResults	= ( pThis->m_StatusWarnErr	.GetCheck() == 1) ? TRUE : FALSE;
	pThis->m_bSendMail			= ( pThis->m_SendMail		.GetCheck() == 1) ? TRUE : FALSE;
	pThis->m_bMailOnEach		= (((CButton*) pThis->GetDlgItem( IDC_EVERYCONFIG	 ))->GetCheck()	== 1) ? TRUE : FALSE;
	pThis->m_bSendFile			= (((CButton*) pThis->GetDlgItem( IDC_SENDFILEASBODY ))->GetCheck() == 1) ? TRUE : FALSE;

	//
	//	WRITE MEMBERS
	//
	AfxGetApp()->WriteProfileString	(_T("Configure"), _T("SendTo"				), this->m_strSendTo		);
	AfxGetApp()->WriteProfileString	(_T("Configure"), _T("StatusText"			), this->m_strStatusLine	);
	AfxGetApp()->WriteProfileInt	(_T("Configure"), _T("MailOnEach"			), this->m_bMailOnEach		);
	AfxGetApp()->WriteProfileInt	(_T("Configure"), _T("SendMail"				), this->m_bSendMail		);
	AfxGetApp()->WriteProfileInt	(_T("Configure"), _T("IncludeBuildResults"	), this->m_bIncBuildResults	);
	AfxGetApp()->WriteProfileInt	(_T("Configure"), _T("SendFile"				), this->m_bSendFile		);
	AfxGetApp()->WriteProfileInt	(_T("Configure"), _T("FinishedAction"		), this->m_iFinishedAction	);

	return TRUE;
}

//
//---------------------------------------------------------------------------------------------------
//****************************************     VIRTUALS     *****************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																				   DoDataExchange
//***************************************************************************************************
//**	@DOC		VIRTUALS
//**	@MFUNC		Exchanges data between the dialog's members and its controls
//**	@PARM		[in/out] Pointer to data exchange context
//**	@END
//***************************************************************************************************
//inline
void CDialogConfigure::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange( pDX );

	//{{AFX_DATA_MAP(CDialogConfigure)
	DDX_Control( pDX, IDC_STATUSWARNERR			, m_StatusWarnErr	);
	DDX_Control( pDX, IDC_STATUSTEXT			, m_StatusText		);
	DDX_Control( pDX, IDC_STATUSLINE			, m_StatusLine		);
	DDX_Control( pDX, IDC_SENDTO				, m_SendTo			);
	DDX_Control( pDX, IDC_SENDTEXT				, m_SendText		);
	DDX_Control( pDX, IDC_SENDMAIL				, m_SendMail		);
	DDX_Control( pDX, IDC_SENDGROUP				, m_SendGroup		);
	DDX_Control( pDX, IDC_FILENAMETEXT			, m_FileName		);
	DDX_Control( pDX, IDC_BROWSE				, m_Browse			);
	DDX_Control( pDX, IDC_BODYGROUP				, m_BodyGroup		);
	DDX_Control( pDX, IDC_BODYFILENAME			, m_BodyFileName	);
	DDX_CBIndex( pDX, IDC_COMBO_FINISH_ACTION	, m_iFinishedAction	);
	//}}AFX_DATA_MAP
}

//
//---------------------------------------------------------------------------------------------------
//****************************************     MESSAGES     *****************************************
//---------------------------------------------------------------------------------------------------
//

BEGIN_MESSAGE_MAP(CDialogConfigure, CDialog)
	//{{AFX_MSG_MAP(CDialogConfigure)
	ON_BN_CLICKED( IDC_WORKSPACES		, OnProjects		)
	ON_BN_CLICKED( IDC_BROWSE			, OnBrowse			)
	ON_BN_CLICKED( IDC_SENDFILEASBODY	, OnSendFileAsBody	)
	ON_BN_CLICKED( IDC_BLANKBODY		, OnBlankBody		)
	ON_BN_CLICKED( IDC_SENDMAIL			, OnSendMail		)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//***************************************************************************************************
//**																					   OnProjects
//***************************************************************************************************
//**	@DOC		MESSAGES
//**	@MFUNC		Called when the Projects button is pressed
//**	@COMM		Brings up the projects dialog
//**	@END
//***************************************************************************************************
//inline
void CDialogConfigure::OnProjects()
{
	//
	//	CHECK AVAILABILITY
	//
	if ( NULL == this->m_pIApplication )
	{
		this->MessageBox( "Application interface not intitialized!", "CDialogConfigure::OnProjects()", MB_ICONERROR );
		return;
	}

	//
	//	DISPLAY THE PROJECTS DIALOG
	//
	CDialogProjects dlgProjects;
	dlgProjects.SetApplication( this->m_pIApplication );
	dlgProjects.DoModal();
}

//***************************************************************************************************
//**																						 OnBrowse
//***************************************************************************************************
//**	@DOC		MESSAGES
//**	@MFUNC		Called when the Browse button is pressed
//**	@COMM		Brings up the file dialog for the mail body
//**	@END
//***************************************************************************************************
//inline
void CDialogConfigure::OnBrowse()
{
	//
	//	LOAD TEXT FILE SPECS
	//
	CString strFileSpec;
			strFileSpec.LoadString( IDS_TEXTFILES );

	//
	//	SHOW BROWSER
	//
	CFileDialog dlgBrowser( TRUE, "", "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFileSpec );

	if ( IDOK != dlgBrowser.DoModal() )
	{
		return;
	}

	//
	//	UPDATE PATHS
	//
	this->m_strFileToSend = dlgBrowser.GetPathName();
	this->m_BodyFileName.SetWindowText( this->m_strFileToSend );
}

//***************************************************************************************************
//**																				 OnSendFileAsBody
//***************************************************************************************************
//**	@DOC		MESSAGES		
//**	@MFUNC		Called when the user enables the SendFileAsBody radio button
//**	@COMM		Enables body file selection controls 
//**	@END
//***************************************************************************************************
//inline
void CDialogConfigure::OnSendFileAsBody()
{
	//
	//	ENABLE BODY SELECTION CONTROLS
	//
	this->m_FileName	.EnableWindow( TRUE );
	this->m_BodyFileName.EnableWindow( TRUE );
	this->m_Browse		.EnableWindow( TRUE );
}

//***************************************************************************************************
//**																					  OnBlankBody
//***************************************************************************************************
//**	@DOC		MESSAGES
//**	@MFUNC		Called when the user enables the BlankBody radio button
//**	@COMM		Disables body file selection controls 
//**	@END
//***************************************************************************************************
//inline
void CDialogConfigure::OnBlankBody()
{
	//
	//	DISABLE BODY SELECTION CONTROLS
	//
	this->m_FileName	.EnableWindow( FALSE );
	this->m_BodyFileName.EnableWindow( FALSE );
	this->m_Browse		.EnableWindow( FALSE );
}

//***************************************************************************************************
//**																					   OnSendMail
//***************************************************************************************************
//**	@DOC		MESSAGES
//**	@MFUNC		Called when the user checks or unchecks the SendMail check box
//**	@COMM		Enables or disables other controls accordingly
//**	@END
//***************************************************************************************************
//inline
void CDialogConfigure::OnSendMail()
{
	//
	//	DETERMINE STATE
	//
	this->m_bSendMail = ( this->m_SendMail.GetCheck() == 1 ) ? TRUE : FALSE;

	//
	//	UPDATE CONTROLS
	//
	this->m_StatusWarnErr	.EnableWindow( this->m_bSendMail );
	this->m_StatusText		.EnableWindow( this->m_bSendMail );
	this->m_StatusLine		.EnableWindow( this->m_bSendMail );
	this->m_BodyFileName	.EnableWindow( this->m_bSendMail );
	this->m_BodyGroup		.EnableWindow( this->m_bSendMail );
	this->m_Browse			.EnableWindow( this->m_bSendMail );
	this->m_SendGroup		.EnableWindow( this->m_bSendMail );
	this->m_SendTo			.EnableWindow( this->m_bSendMail );
	this->m_SendText		.EnableWindow( this->m_bSendMail );
 
	((CButton*) this->GetDlgItem( IDC_EVERYCONFIG		))->EnableWindow( this->m_bSendMail );
	((CButton*) this->GetDlgItem( IDC_ENTIREWORKSPACE	))->EnableWindow( this->m_bSendMail );
	((CButton*) this->GetDlgItem( IDC_SENDFILEASBODY	))->EnableWindow( this->m_bSendMail );
	((CButton*) this->GetDlgItem( IDC_BLANKBODY			))->EnableWindow( this->m_bSendMail );
	((CButton*) this->GetDlgItem( IDC_FILENAMETEXT		))->EnableWindow( this->m_bSendMail );

	if ( TRUE == this->m_bSendFile )
	{
		((CButton*) this->GetDlgItem( IDC_SENDFILEASBODY ))->SetCheck( 1 );

		this->m_FileName	.EnableWindow( TRUE );
		this->m_BodyFileName.EnableWindow( TRUE );
		this->m_Browse		.EnableWindow( TRUE );
	}
	else
	{
		((CButton*) this->GetDlgItem( IDC_BLANKBODY ))->SetCheck( 1 );

		this->m_FileName	.EnableWindow(FALSE);
		this->m_BodyFileName.EnableWindow(FALSE);
		this->m_Browse		.EnableWindow(FALSE);
	}
}

//***************************************************************************************************
//**																							 OnOK
//***************************************************************************************************
//**	@DOC		MESSAGES
//**	@MFUNC		Called when the OK button is pressed
//**	@COMM		Writes the profile and closes the dialog
//**	@END
//***************************************************************************************************
//inline
void CDialogConfigure::OnOK()
{
	//
	//	WRITE PROFILE
	//
	if ( FALSE == this->WriteProfile() )
	{
		return;
	}

	//
	//	CALL BASE CLASS' IMPLEMENTATION
	//
	CDialog::OnOK();
}

//***************************************************************************************************
//**																					 OnInitDialog
//***************************************************************************************************
//**	@DOC		MESSAGES
//**	@MFUNC		Called to initialize the dialog
//**	@END
//***************************************************************************************************
//inline
BOOL CDialogConfigure::OnInitDialog()
{
	//
	//	CALL BASE CLASS' IMPLEMENTATION
	//
	CDialog::OnInitDialog();

	//
	//	READ PROFILE
	//
	this->LoadProfile();

	//
	//	ADJUST CONTROLS
	//
	this->m_SendMail.SetCheck( this->m_bSendMail );

	if ( TRUE == this->m_bIncBuildResults )	this->m_StatusWarnErr.SetCheck( 1 );
	else									this->m_StatusWarnErr.SetCheck( 0 );

	if ( TRUE == this->m_bMailOnEach )		((CButton*) this->GetDlgItem(IDC_EVERYCONFIG	))->SetCheck( 1 );
	else									((CButton*) this->GetDlgItem(IDC_ENTIREWORKSPACE))->SetCheck( 1 );

	this->m_BodyFileName.SetWindowText( this->m_strFileToSend );

	if (this->m_bSendFile == TRUE)
	{
		((CButton*) this->GetDlgItem( IDC_SENDFILEASBODY ))->SetCheck( 1 );

		this->m_FileName	.EnableWindow( TRUE );
		this->m_BodyFileName.EnableWindow( TRUE );
		this->m_Browse		.EnableWindow( TRUE );
	}
	else
	{
		((CButton*)	this->GetDlgItem( IDC_BLANKBODY ))->SetCheck( 1 );

		this->m_FileName	.EnableWindow( FALSE );
		this->m_BodyFileName.EnableWindow( FALSE );
		this->m_Browse		.EnableWindow( FALSE );
	}

	this->m_SendTo			.SetWindowText( this->m_strSendTo		);
	this->m_StatusLine		.SetWindowText( this->m_strStatusLine	);

	this->m_StatusWarnErr	.EnableWindow( this->m_bSendMail );
	this->m_StatusText		.EnableWindow( this->m_bSendMail );
	this->m_StatusLine		.EnableWindow( this->m_bSendMail );

	this->m_BodyGroup		.EnableWindow( this->m_bSendMail );
	this->m_SendGroup		.EnableWindow( this->m_bSendMail );
	this->m_SendTo			.EnableWindow( this->m_bSendMail );
	this->m_SendText		.EnableWindow( this->m_bSendMail );

	((CButton*) this->GetDlgItem( IDC_EVERYCONFIG		))->EnableWindow( this->m_bSendMail );
	((CButton*) this->GetDlgItem( IDC_ENTIREWORKSPACE	))->EnableWindow( this->m_bSendMail );
	((CButton*) this->GetDlgItem( IDC_SENDFILEASBODY	))->EnableWindow( this->m_bSendMail );
	((CButton*) this->GetDlgItem( IDC_BLANKBODY			))->EnableWindow( this->m_bSendMail );
	((CButton*) this->GetDlgItem( IDC_FILENAMETEXT		))->EnableWindow( this->m_bSendMail );

	this->m_SendTo.SetWindowText( this->m_strSendTo );

	//
	//	RETURN 
	//
	return TRUE;  
}

